package com.michaellou.memcache;

import java.util.Date;

import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;

//import org.apache.log4j.LogManager;
//import org.apache.log4j.Logger;

public class Memcache {

 static SockIOPool pool = null;
 static MemCachedClient mc;
// private static Logger logger = LogManager.getLogger(Memcache.class);
    static {
		  try {
			  String[] serverlist=new String[]{"116.211.20.207:11211"};
		    pool = SockIOPool.getInstance();
		    pool.setServers(serverlist);
		    pool.setInitConn(10);
		    pool.setMinConn(5);
		    pool.setMaxConn(100);
		    pool.setMaintSleep(100);
		    pool.setNagle(false);
		    boolean flag = pool.isInitialized();
		    if(!flag)
		    {
		       pool.initialize();
		    }
		    mc = new MemCachedClient();
		    mc.setCompressEnable(false);
		    mc.setCompressThreshold(0);
		   
		  } catch (Exception e) {
		//   logger.error(e);
		  }
 }
/*
 /**
  * 设置cache
  * 
  * @param key
  * @param value
  * @param mill
  */
	
 public static void setCache(String key, Object value, long mill) {
   if (mill == 0) {
   mc.set(key, value);
  } else {
   mc.set(key, value, new Date(mill));
  }
 }

 /**
  * 获取cache内容
  * 
  * @param key
  * @return
  */
	
 public static Object getCache(String key) {
  Object obj = mc.get(key);
  return obj;
 }

 /**
  * 删除cache
  * 
  * @param key
  */
	
 public static void delCache(String key) {
  mc.delete(key);
 }

 public static void main(String args[]) {
    
  Memcache.setCache("user2","116.211.20.207:11211",30*60*1000);
  Memcache.setCache("user1", "AAAAAAAABBBBBBBBBBBBBCCCCCCCCCCCDDDDDDDDDDDDEEEEEEEEEEEEEEFFFFFFFFF", 0);
  System.out.println(Memcache.getCache("user2"));
  System.out.println(Memcache.getCache("user1"));
     
 }
}